package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.pojo.*;
import com.gxa.zhonglong.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@CrossOrigin
@RestController
@Api(value = "固定编码入库")
@RequestMapping("/FixedCoding")
public class FixedCodingController {

    @Autowired
    GoodsService goodsService = null;

    @Autowired
    AuditService auditService = null;

    @Autowired
    FixedCodingService fixedCodingService = null;

    @Autowired
    StoreBarterService storeBarterService = null;

    @Autowired
    SupplierService supplierService = null;


    @PostMapping("/putFixedCoding")
    @ApiOperation(value ="新增固定编码入库" )
    public Map insertFixedCoding(@RequestBody FixedCodingAndAuditVo fixedCodingAndAuditVo) {

        FixedCoding fixedCoding = fixedCodingAndAuditVo.getFixedCoding();
        Audit audit = fixedCodingAndAuditVo.getAudit();
        fixedCodingService.insertFixedCoding(fixedCoding);
        auditService.insertAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getSupplierAndStore")
    @ApiOperation(value = "查询supplier表和store表的数据")
    public Map selectSupplierAndStore() throws Exception {
        //获取门店信息
        List<String> store = storeBarterService.selectAllStore();
        //获取供应商信息
        List<Supplier> suppliers = supplierService.selectAll();

        Map<String,List> map = new HashMap<>();

        map.put("store",store);
        map.put("suppliers",suppliers);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(map);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getCounter")
    @ApiOperation(value = "根据门店信息查询counter(柜台/类库名称)表的数据")
    public Map selectCounter(@RequestParam("barterName")  String barterName) throws Exception {

        List<String> counter = storeBarterService.selectCounter(barterName);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(counter);
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getFixedCoding")
    @ApiOperation(value = "根据条件进行多表的联合查询")
    public Map selectByWhere(@RequestBody Where where){
        List<Map<String, Object>> selectByWhere = fixedCodingService.selectByWhere(where);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("selectByWhere",selectByWhere);
        return jsonResult.getDataMap();
    }


    @PostMapping("/getByReceiptNumber")
    @ApiOperation(value = "根据单据编号查看商品表的记录")
    public Map selectByReceiptNumber(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> maps = fixedCodingService.selectByReceiptNumberGoods(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }

    @PostMapping("/getByReceiptNumberFixedCoding")
    @ApiOperation(value = "根据单据编号查看固定编码入库表的记录")
    public Map getByReceiptNumberFixedCoding(@RequestParam("receiptNumber") String receiptNumber){
        List<Map<String, Object>> maps = fixedCodingService.selectByReceiptNumberFixedCoding(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }


    @PostMapping("/deleteByFixedCoding")
    @ApiOperation(value = "根据单据编号删除固定编码入库表的记录")
    public Map deleteByInventory(@RequestParam("receiptNumber") String receiptNumber){

        fixedCodingService.deleteByReceiptNumberFixedCoding(receiptNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


    @PostMapping("/setAudit")
    @ApiOperation(value = "根据单据编号更新审批表的记录")
    public Map updateByAudit(@RequestBody Audit audit) throws BusinessException {

        auditService.updateByReceiptNumberAudit(audit);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/putFixedCodingGoods")
    @ApiOperation(value = "根据商品编码添加固定编码商品明细")
    public Map updateByAudit(@RequestBody Map map){

        String receiptNumber =(String) map.get("receiptNumber");
        String number =(String) map.get("number");
        Goods goods = goodsService.selectGoods(number);
        goods.setReceiptNumber(receiptNumber);
        fixedCodingService.insertFixedCodingGD(goods);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteByGoodsNumber")
    @ApiOperation(value = "根据商品编码删除商品记录")
    public Map deleteByGoodsNumber(@RequestParam("number") String number ) throws BusinessException {
        fixedCodingService.deleteByNumber(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }


}
